package nuonuo.com.view.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ListView;

/**
 * Created by chenjun on 2017/3/6.
 */

public class MyListView extends ListView {
    private int mLastX,mLastY;
    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int mInterceptTempX;
    private int mInterceptTempY;
    private boolean intercept;
    private boolean hasInterceptMove;
    private boolean hasStepMove;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN){
            hasInterceptMove = false;
            hasStepMove = false;
        }
        mInterceptTempX = (int) ev.getX();
        mInterceptTempY = (int) ev.getY();
        if (ev.getAction() == MotionEvent.ACTION_MOVE){
            if (hasInterceptMove){
                intercept = true;
            }else if (hasStepMove){
                intercept = false;
            }else if (Math.pow(mInterceptTempX - mLastX,2) + Math.pow(mInterceptTempY - mLastY,2) <= Math.pow(ViewConfiguration.get(getContext()).getScaledTouchSlop(),2)){
                intercept = false;
            }else {
                if (Math.sqrt(3) * Math.abs(mInterceptTempX - mLastX) > Math.abs(mInterceptTempY - mLastY)) {
                    intercept = false;
                    hasStepMove = true;
                }else {
                    intercept = true;
                    hasInterceptMove = true;
                }
            }
        }else {
            intercept = super.onInterceptTouchEvent(ev);
        }
        mLastX = mInterceptTempX;
        mLastY = mInterceptTempY;
        return intercept;
    }
}
